﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarLoanSystem.Entity;

namespace CarLoanSystem
{
    public partial class frmDebtInfo : Form
    {
        private static DeptInfoItem deptItem;
        private static string mode;
        private static LoanItem loanItem;
        public frmDebtInfo(int loanId, string unPaid)
        {
            InitializeComponent();
            loanItem = DAL.LoanInfoDAL.LoadLoan(loanId);
            this.txtLoanId.Text = loanItem.LOAN_ID.ToString();
            this.txtPayMonth.Text = loanItem.PAY_MONTH.ToString();
            this.txtDown.Text = loanItem.DOWN_PAYMENT.ToString();
            this.txtRestMoney.Text = loanItem.DEBT_MONTH_REST.ToString();
            this.txtUnPaid.Text = unPaid;
            this.txtVehicleId.Text = loanItem.VEHICLE_ID;

            deptItem = DAL.DeptInfoDAL.LoadDept(loanItem.LOAN_ID);

            CustomerItem cusItem = DAL.CustomerInfoDAL.LoadCustomer(loanItem.CUSTOMER_ID);
            this.txtName.Text = cusItem.CUSTOMER_NAME;
            this.txtLastName.Text = cusItem.CUSTOMER_LASTNAME;

            InterrestInfoItem InterrestItem = DAL.InterrestInfoDAL.LoadInterrest(loanItem.INTERREST_ID);
            this.txtInterrest.Text = InterrestItem.INTERREST_RATE.ToString() + "%";

            CarInfoItem carItem = DAL.CarInfoDAL.LoadCar(loanItem.CAR_ID);
            this.txtBrand.Text = carItem.CAR_BRAND_NAME;
            this.txtModel.Text = carItem.CAR_MODEL_NAME;
            this.txtPrice.Text = carItem.CAR_PRICE.ToString();
            
            if(deptItem.DEBT_STATUS == "A")
            {
                this.cbbStatusDebt.SelectedIndex = 0;
                mode = "A";
            }
            else if(deptItem.DEBT_STATUS == "B")
            {
                this.cbbStatusDebt.SelectedIndex = 1;
                this.cbbStatusDebt.Enabled = false;
                this.btnAddContact.Visible = false;
                mode = "A";
            }
            else
            {
                this.cbbStatusSequester.Visible = true;
                this.lbStatusSequester.Visible = true;
                this.cbbStatusDebt.Visible = false;
                this.lbStatusDebt.Visible = false;
                this.txtSaleCar.Visible = true;
                this.lbPricesell.Visible = true;
                this.lblBath.Visible = true;
                mode = "B";
            }

            if (deptItem.DEBT_STATUS == "C")
            {
                this.cbbStatusSequester.SelectedIndex = 0;
            }
            else if (deptItem.DEBT_STATUS == "D")
            {
                this.cbbStatusSequester.SelectedIndex = 1;
            }
            else if (deptItem.DEBT_STATUS == "E")
            {
                this.cbbStatusSequester.SelectedIndex = 2;
            }
            this.grvDetail.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellContentClick);
            genGridview();
        }

        private void genGridview()
        {
            // gen Grideview
            grvDetail.AutoGenerateColumns = false;

            //Set Columns Count
            grvDetail.ColumnCount = 4;

            grvDetail.Columns[0].Width = 45;

            //Add Columns
            grvDetail.Columns[1].Name = "DEBT_DETAIL_ID";
            grvDetail.Columns[1].HeaderText = "รหัส";
            grvDetail.Columns[1].DataPropertyName = "DEBT_DETAIL_ID";
            grvDetail.Columns[1].Width = 45;

            grvDetail.Columns[2].Name = "DEBT_DETAIL_DATE";
            grvDetail.Columns[2].HeaderText = "วันที่ติดต่อ";
            grvDetail.Columns[2].DataPropertyName = "DEBT_DETAIL_DATE";
            grvDetail.Columns[2].Width = 90;

            grvDetail.Columns[3].Name = "DEBT_DETAIL";
            grvDetail.Columns[3].HeaderText = "รายละเอียดการติดต่อ";
            grvDetail.Columns[3].DataPropertyName = "DEBT_DETAIL";
            grvDetail.Columns[3].Width = 190;
            // end gen gridview
            grvDetail.DataSource = LoadDeptDetail();
        }

        private void CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))
            {
                frmDebtInfoDetail frmDebtInfoDetail = new frmDebtInfoDetail(Convert.ToInt32(((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value.ToString()), deptItem.DEBT_ID);
                frmDebtInfoDetail.ShowDialog();
            }
        }

        private void btnAddContact_Click(object sender, EventArgs e)
        {
            frmDebtInfoDetail frmDebtInfoDetail = new frmDebtInfoDetail(deptItem.DEBT_ID);
            frmDebtInfoDetail.ShowDialog();
            grvDetail.DataSource = LoadDeptDetail();
        }

        private DataTable LoadDeptDetail()
        {
            DataTable dt = DAL.DeptDetailInfoDAL.LoadAllDeptDetail(deptItem.DEBT_ID);
            return dt;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (mode == "A")
            {
                if (cbbStatusDebt.SelectedItem.ToString().Equals("อยู่ระหว่างการทวงหนี้สิน"))
                {
                    deptItem.DEBT_STATUS = "A";
                }
                else if (cbbStatusDebt.SelectedItem.ToString().Equals("ส่งเรื่องให้พนักงานกฏหมาย"))
                {
                    deptItem.DEBT_STATUS = "B";
                }
                DAL.DeptInfoDAL.UpdateDeptStatus(deptItem);
                loanItem.LOAN_ID = deptItem.LOAN_ID;
                loanItem.DEBT_STATUS = deptItem.DEBT_STATUS;
                DAL.LoanInfoDAL.UpdateDeptStatus(loanItem);
                if (deptItem.DEBT_STATUS == "B")
                {
                    LitigationInfoItem LitigationInfoItem = new LitigationInfoItem();
                    LitigationInfoItem.EMPLOYEE_ID = DAL.EmployeeInfoDAL.LoadEmployeeLitigation();
                    LitigationInfoItem.LITIGATION_STATUS = "A";
                    LitigationInfoItem.LOAN_ID = deptItem.LOAN_ID;
                    DAL.LitigationInfoDAL.Insert(LitigationInfoItem);

                    EmployeeItem empItem = DAL.EmployeeInfoDAL.LoadEmployee(LitigationInfoItem.EMPLOYEE_ID.ToString());
                    AlertsMessageItem alertItem = new AlertsMessageItem();
                    alertItem.ALERTS_MESSAGE_DETAIL = "แจ้งเตือนการดำเนินเรื่องทางกฎหมายของการขอกู้รหัส " + LitigationInfoItem.LOAN_ID;
                    alertItem.ALERTS_MESSAGE_STATUS = "A";
                    alertItem.USER_NAME = empItem.USERNAME;
                    alertItem.LOAN_ID = LitigationInfoItem.LOAN_ID;
                    DAL.AlertsMessageInfoDAL.Insert(alertItem);
                }
            }
            else if (mode == "B")
            {
                SequesterItem sequesterItem = DAL.SequesterInfoDAL.LoadSequester(deptItem.LOAN_ID);
                sequesterItem.SEQUESTER_DATE = DateTime.Now;
                if (cbbStatusSequester.SelectedItem.ToString().Equals("อยู่ระหว่างการยึดรถคืน"))
                {
                    sequesterItem.SEQUESTER_STATUS = "A";
                    deptItem.DEBT_STATUS = "C";
                }
                else if (cbbStatusSequester.SelectedItem.ToString().Equals("ยึดรถคืนแล้ว"))
                {
                    sequesterItem.SEQUESTER_STATUS = "B";
                    deptItem.DEBT_STATUS = "D";
                }
                else if (cbbStatusSequester.SelectedItem.ToString().Equals("ขายรถเรียบร้อยแล้ว"))
                {
                    if (this.txtSaleCar.Text.Equals(""))
                    {
                        MessageBox.Show("กรุณากรอกราคาขายรถ", "ข้อมูลการทวงหนี้สิน");
                        return;
                    }
                    else 
                    {
                        sequesterItem.CAR_PRICE = Convert.ToInt32(this.txtSaleCar.Text);
                    }
                    deptItem.DEBT_STATUS = "E";
                    sequesterItem.SEQUESTER_STATUS = "C";
                }
                DAL.DeptInfoDAL.UpdateDeptStatus(deptItem);
                loanItem.LOAN_ID = deptItem.LOAN_ID;
                loanItem.DEBT_STATUS = deptItem.DEBT_STATUS;
                DAL.LoanInfoDAL.UpdateDeptStatus(loanItem);
                DAL.SequesterInfoDAL.UpdateSequesterStatus(sequesterItem);
                CustomerItem customerItem = new CustomerItem();
                customerItem.CUSTOMER_ID = loanItem.CUSTOMER_ID;
                customerItem.CUSTOMER_BLACK_LIST = "Y";
                DAL.CustomerInfoDAL.UpdateBalckList(customerItem);
            }
            MessageBox.Show("บันทึกข้อมูลสำเร็จ", "ข้อมูลการทวงหนี้สิน");
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void grvDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
